package com.proj.core;

import org.springframework.beans.BeansException;
import org.springframework.context.ApplicationContext;
import org.springframework.context.ApplicationContextAware;

public final class ServiceFactory implements ApplicationContextAware
{
    private static ServiceFactory serviceFactory = new ServiceFactory();

    private ServiceFactory()
    {
        System.out.println("ServiceFactory.ServiceFactory");
    }

    private ApplicationContext context;

    public void setApplicationContext(ApplicationContext applicationContext) throws BeansException
    {
        System.out.println("ServiceFactory.setApplicationContext");
        this.context = applicationContext;
    }

    public static ServiceFactory getServiceFactory()
    {
        System.out.println("ServiceFactory.getServiceFactory");
        return serviceFactory;
    }

    public Object getBean(String name)
    {
        System.out.println("ServiceFactory.getBean");
        System.out.println("name = " + name);
        if (context.containsBean(name))
        {
            return context.getBean(name);
        }
        else
        {
            throw new RuntimeException("Bean not defined");
        }
    }
}
